<?php

class PostController extends FrontendController {
    public $layout = '//layouts/blog';
    public $includeText = ' với mong muốn luôn làm hài lòng quý khách khi đến với chúng tôi. Hãy gọi ngay: 09.777.5.7.9.11';
    public $includeTitleText = 'Thiet ke web3in1.com - Thiết kế web giá rẻ - SEO có ngay pagerank | ';

    public function actionIndex($lang = 'vi', $username = '', $tag = '', $date = '', $kw = '') {
        $page = Helper::get('page') ? Helper::get('page') : 1;
        $dateA = $date ? date('Y-m-d', Helper::changeDateToFormat($date, 'm-d-Y')) : '';

        $allPost = CreateCache::usingCache('Post', false, '', $lang, 'ordering', $tag, $dateA, $kw, $username);
        if ($tag) {
            $params = array('key' => 'tag', 'value' => $tag);
            $url       = Helper::url('/Blog/post/index', array('lang' => $lang, 'tag' => $tag));
            $paging    = new Paging($url, count($allPost), ITEM_PER_PAGE, $page, 10, $params);
            $pageTitle = Yii::app()->language == 'en' ? 'The posts with tags " ' . $tag . ' "' : 'Những bài viết với tags " ' . $tag . '"';
        } elseif ($username) {
            $params = array('key' => 'username', 'value' => $username);
            $url       = Helper::url('/Blog/post/index', array('lang' => $lang, 'username' => $username));
            $paging    = new Paging($url, count($allPost), ITEM_PER_PAGE, $page, 10, $params);
            $pageTitle = Yii::app()->language == 'en' ? 'The posts of user " ' . $username . ' "' : 'Những bài viết của user " ' . $username . '"';
        } elseif ($date) {
            $params = array('key' => 'date', 'value' => $date);
            $url        = Helper::url('/Blog/post/index', array('lang' => $lang, 'date' => $date));
            $paging     = new Paging($url, count($allPost), ITEM_PER_PAGE, $page, 10, $params);
            $pageTitle  = Yii::app()->language == 'en' ? 'The posts in date " ' . $date . '"' : 'Những bài viết trong ngày " ' . $date . '"';
        } elseif ($kw) {
            $params = array('key' => 'kw', 'value' => $kw);
            $url       = Helper::url('/Blog/post/index', array('lang' => $lang, 'kw' => $kw));
            $paging    = new Paging($url, count($allPost), ITEM_PER_PAGE, $page, 10, $params);
            $pageTitle = Yii::app()->language == 'en' ? 'The posts of search result with keyword " ' . $kw . '"' : 'Những bài viết tìm được với từ khóa " ' . $kw . '"';
        } else {
            $url       = Helper::url('/Blog/post/index', array('lang' => $lang));
            $paging    = new Paging($url, count($allPost), ITEM_PER_PAGE, $page, 10);
            $pageTitle = Yii::app()->language == 'en' ? 'The posts ' : 'Những bài viết';
        }
        $limit     = array('begin' => $paging->begin, 'end' => $paging->rows_in_page);
        $cachePost = CreateCache::usingCache('Post', false, '', $lang, 'ordering', $tag, $dateA, $kw, $username, Cache_Time, $limit, $page);

        // seo
        $this->breadcrumbs = array(
            $pageTitle
        );
        $pageTitle = $this->pageTitle   = $this->includeTitleText . $pageTitle;
        $keywords =  str_replace(' - ', ',', $pageTitle);

        $seo               = array(
            'keywords'    => $keywords . ($tag ? $tag : ($date ? date : $kw)),
            'description' => $pageTitle . ',' . DEFAULT_META_DESCRIPTION,
            'title'       => $pageTitle,
            'type'        => 'Blog',
            'url'         => Yii::app()->createAbsoluteUrl($url),
            'image'       => !empty($cachePost) ? $cachePost[0]->image : 'uploads/avatar/2013_04_07_19_05_55_avatar.png',
            'site_name'   => $pageTitle,
        );
        Helper::seo($seo);

        $this->render('index', array(
            'cachePost' => $cachePost,
            'pg'        => $paging,
            'lang'      => $lang
        ));
    }

    public function actionView($postAlias, $lang = 'vi') {
        $cachePost = CreateCache::usingCache('Post', false, $postAlias, $lang);
        if (!$cachePost) {
            throw new CHttpException(400, Helper::t('Error_Link'));
        }
        $pageTitle = $cachePost->title;
        // older cachePost
        $olderPosts = CreateCache::usingCache('Post', false, '', $lang, 'ordering', '', '', '', '', Cache_Time, 5, 1, $cachePost->id);

        // action for Comments
        $modelComment = new Comment();
        // Uncomment the following line if AJAX validation is needed
        Helper::performAjaxValidation($modelComment, 'comment-form');
        if (Helper::post('Comment')) {
            throw new CHttpException(400, Helper::t('Error_Link'));

            $modelComment->attributes = Helper::post('Comment');
            $modelComment->parent_id  = Helper::post('current') ? Helper::post('current') : 0;
            if ($modelComment->save()) {
                $scriptLoadPage = 'parent.window.location = parent.window.location; return false;';
                Helper::cs()->registerScript('scriptLoadPage', $scriptLoadPage, CClientScript::POS_END);
            }
        }

        // seo
        $this->breadcrumbs = array(
            $pageTitle
        );
        $pageTitle = $this->pageTitle = $this->includeTitleText . $pageTitle;
        $keywords =  str_replace(' - ', ',', $pageTitle);

        $seo               = array(
            'keywords'    => $keywords,
            'description' => $pageTitle . ', ' . $cachePost->info . ' |' . $this->includeText,
            'title'       => $pageTitle . ', ' . $cachePost->info,
            'type'        => 'Blog',
            'url'         => Yii::app()->createAbsoluteUrl(Helper::url('/Blog/post/view', array('postAlias' => $postAlias, 'lang' => $lang))),
            'image'       => !empty($cachePost->image) ? Helper::renderImage($cachePost->image, 'uploads/Post', ',',  true, true) : 'uploads/avatar/2013_04_07_19_05_55_avatar.png',
            'site_name'   => $pageTitle,
        );
        Helper::seo($seo);

        $this->render('view', array(
            'cachePost'  => $cachePost,
            'olderPosts' => $olderPosts,
            'modelComment' => $modelComment,
            'lang' => $lang
        ));
    }

    public function actionCalendar($lang = 'vi') {
        $data = array();

        $allPost = CreateCache::usingCache('Post', false, '', $lang);
        if (count($allPost)) {
            $data['color'] = 'color-blue';

            foreach ($allPost as $post) {
                $data['create_date'][] = $post->create_date;
            }
        }

        echo CJSON::encode($data);
        Yii::app()->end();
    }
}